﻿using System;

namespace GodProject.console
{
  /// <summary>
  /// Represents God console. Reads user input, create and print peoples.
  /// Can not work on Sundaya
  /// </summary>
  class GodConsole
  {
    /// <summary>
    /// Prints info to console
    /// </summary>
    private GodConsoleWriter _writer;

    /// <summary>
    /// Creates peoples
    /// </summary>
    private GodConsoleController _controller;

    /// <summary>
    /// Default constructor
    /// </summary>
    public GodConsole()
    {
      ColorScheme colorScheme = new ColorScheme();
      _writer = new GodConsoleWriter(colorScheme);
      _controller = new GodConsoleController(_writer);
      TryInitialize();
    }

    /// <summary>
    /// Initialize god console. If today is Sunday, throws exception
    /// <exception cref="SundayDayException">Thrown if today is Sunday</exception>
    /// </summary>
    private void TryInitialize()
    {
      if (!CheckIfWorking)
      {
        throw new SundayDayException();
      }

      _writer.WritePrompt();
    }

    /// <summary>
    /// Checks if god console can work (tests if today is not Sunday)
    /// </summary>
    private static bool CheckIfWorking
    {
      get { return DateTime.Today.DayOfWeek != DayOfWeek.Sunday; }
    }

    /// <summary>
    /// Runs console. Reads input, generates and prints people
    /// <exception cref="WrongInputException">Thrown if user input is not a number</exception>
    /// </summary>
    public void Run()
    {
      int count;
      string readed = Console.ReadLine();
      bool ok = Int32.TryParse(readed, out count);
      if (!ok)
      {
        throw new WrongInputException();
      }

      _writer.WriteStartCreation();
      _controller.StartCreation(count);
    }

  }
}
